﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.Sim.Admin.AdminSpecificPermission;
using Dapper;

namespace BCData.Sim.Admin.AdminSpecificPermission
{
    public class AdminSpecificPermissionData : IAdminSpecificPermissionData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT AdminSpecificPermissionId,SpecificPermissionName,SpecificPermissionKey FROM AdminSpecificPermission";

        public AdminSpecificPermissionData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<bool> CheckExists(int adminSpecificPermissionId)
        {
            string sql = @"SELECT 1 FROM AdminSpecificPermission WHERE AdminSpecificPermissionId=?AdminSpecificPermissionId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?AdminSpecificPermissionId", adminSpecificPermissionId, DbType.Int32, size: 11);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<bool> CheckExists(string extensionPermissionKey)
        {
            string sql = @"SELECT 1 FROM AdminSpecificPermission WHERE SpecificPermissionKey=?SpecificPermissionKey";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SpecificPermissionKey", extensionPermissionKey, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<AdminSpecificPermissionEntity> Get(int adminSpecificPermissionId)
        {
            string sql = string.Format(@"{0} WHERE AdminSpecificPermissionId=?AdminSpecificPermissionId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?AdminSpecificPermissionId", adminSpecificPermissionId, DbType.Int32, size: 11);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<AdminSpecificPermissionEntity>(sql, parameters);
        }

        public async Task<AdminSpecificPermissionEntity> Get(string extensionPermissionKey)
        {
            string sql = string.Format(@"{0} WHERE SpecificPermissionKey=?SpecificPermissionKey", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SpecificPermissionKey", extensionPermissionKey, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<AdminSpecificPermissionEntity>(sql, parameters);
        }

        public async Task<IEnumerable<AdminSpecificPermissionEntity>> Query()
        {
            return await this.databaseContext.Slave.QueryAsync<AdminSpecificPermissionEntity>(QUERY);
        }
    }
}
